# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
import time

local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


class DistrictDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertDist(self, info):
        """
        添加 district 信息
        :param info:
        :return:
        """
        distName = info['distName']
        cityId = info['cityId']
        distUpdateTime = local_time

        sql = "insert into zzh_district (district_name, city_id, district_updated_time) VALUES " \
              "(%s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [distName, cityId, distUpdateTime])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findDistById(self, distId):
        """
        根据ID查询 district 信息
        :param distId:
        :return:
        """
        sql = "select * from zzh_district WHERE cistrict_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [distId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.district_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findDistByCondition(self, **kwargs):
        """
        district 表的条件查询
        :param kwargs:
        :return:
        """
        cityId = kwargs['cityId']

        sql = "select * from zzh_district WHERE city_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [cityId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.district_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def deleteDist(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_district WHERE district_id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateDist(self, info):
        """
        更新 district 表
        :param info:
        :return:
        """
        distId = info['distId']
        distName = info['distName']
        cityId = info['cityId']
        distUpdateTime = local_time

        sql = "update zzh_district set district_name=%s,city_id=%s,district_updated_time=%s WHERE district_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [distName,cityId, distUpdateTime, distId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectDist(self, distId):
        """
        根据ID查询，返回元组
        :param distId:
        :return: data tuple
        """
        sql = "select * from zzh_district WHERE district_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [distId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr